package wsservice;

import com.alibaba.druid.util.StringUtils;
import entity.*;
import entityr.WebServiceResponse;
import mapper.*;
import model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import utility.XMLUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
public class MacBLL {

    @Autowired
    DbServerMapper dbServerMapper;
    @Autowired
    BerthStateMapper berthStateMapper;
    @Autowired
    BerthStateHisMapper berthStateHisMapper;
    @Autowired
    WipEntityTagMapper wipEntityTagMapper;
    @Autowired
    LotUploadMapper lotUploadMapper;
    @Autowired
    DataUploadMapper dataUploadMapper;
    @Autowired
    AlarmUploadMapper alarmUploadMapper;

    DateTimeFormatter dfISO = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public WebServiceResponse isHasAuthority(UserControl userControl) {
        WebServiceResponse webServiceResponse = new WebServiceResponse();
        Integer i = dbServerMapper.isHasAuthority(userControl.getMacCode(), userControl.getEmpCode());
        if (i > 0) {
            webServiceResponse.setErrorCode("0");
            webServiceResponse.setErrorMsg("OK");
            webServiceResponse.setResultData(null);
        } else {
            webServiceResponse.setErrorCode("-1");
            webServiceResponse.setErrorMsg("NG");
            webServiceResponse.setResultData(null);
        }
        return webServiceResponse;
    }

    public WebServiceResponse Ingredients(Ingredients mod) {
        WebServiceResponse webServiceResponse = new WebServiceResponse();
        String timestampNow = LocalDateTime.now().format(dfISO);
        //
        List<Item> list = dbServerMapper.getItemsByWipEntity(mod.getWipEntity());
        if (list.isEmpty()) {
            webServiceResponse.setErrorCode("-1");
            webServiceResponse.setErrorMsg("NG");
            webServiceResponse.setResultData(null);
            return webServiceResponse;
        }
        for (Item item : list) {
            item.setTimeStamp(timestampNow);
        }
        Item[] datas = new Item[list.size()];
        list.toArray(datas);
        mod.setItem(datas);
        webServiceResponse.setErrorCode("0");
        webServiceResponse.setErrorMsg("OK");
        webServiceResponse.setResultData(XMLUtil.ObjectToXml(mod));
        return webServiceResponse;
    }

    public WebServiceResponse LotDataUpload(LotDataUpload mod) {
        WebServiceResponse webServiceResponse = new WebServiceResponse();
        String timestampNow = LocalDateTime.now().format(dfISO);
        //
        if (mod != null && mod.getPnlNum() != null) {
            for (PNLNum pnlNum : mod.getPnlNum()) {
                if (pnlNum.getItems() != null && pnlNum.getItems().length > 0) {
                    for (Item item : pnlNum.getItems()) {
                        LotUpload lotUpload = new LotUpload();
                        lotUpload.setMacCode(mod.getMacCode());
                        lotUpload.setWipEntity(mod.getWipEntity());
                        lotUpload.setWipEntityPnl(pnlNum.getWipEntityPnl());
                        lotUpload.setTagCode(item.getTagCode());
                        lotUpload.setTagValue(item.getTagValue());
                        lotUpload.setTimeStamp(item.getTimeStamp());
                        lotUploadMapper.InsertLotUpload(lotUpload);
                    }
                }
            }
        }
        //
        webServiceResponse.setErrorCode("0");
        webServiceResponse.setErrorMsg("OK");
        webServiceResponse.setResultData(null);
        return webServiceResponse;
    }

    public WebServiceResponse DataUpload(DataUpload mod) {
        WebServiceResponse webServiceResponse = new WebServiceResponse();
        String timestampNow = LocalDateTime.now().format(dfISO);
        //
        if (mod != null && mod.getItem() != null && mod.getItem().length > 0) {
            for (Item item : mod.getItem()) {
                model.UploadData upload = new model.UploadData();
                upload.setMacCode(mod.getMacCode());
                upload.setWipEntity(mod.getWipEntity());
                upload.setTagCode(item.getTagCode());
                upload.setTagValue(item.getTagValue());
                upload.setTimeStamp(item.getTimeStamp());
                dataUploadMapper.Insert(upload);
            }
        }
        //
        webServiceResponse.setErrorCode("0");
        webServiceResponse.setErrorMsg("OK");
        webServiceResponse.setResultData(null);
        return webServiceResponse;
    }

    public WebServiceResponse AlarmUpload(AlarmUpload mod) {
        WebServiceResponse webServiceResponse = new WebServiceResponse();
        String timestampNow = LocalDateTime.now().format(dfISO);
        //
        if (mod != null && mod.getItem() != null && mod.getItem().length > 0) {
            for (Item item : mod.getItem()) {
                UploadAlarm alarm = new UploadAlarm();
                alarm.setMacCode(mod.getMacCode());
                alarm.setAlarmCode(mod.getAlarmCode());
                alarm.setAlarmDesc(mod.getAlarmDesc());
                alarm.setAlarmStamp(mod.getTimeStamp());
                alarm.setTagCode(item.getTagCode());
                alarm.setTagValue(item.getTagValue());
                alarm.setTimeStamp(item.getTimeStamp());
                alarmUploadMapper.Insert(alarm);
            }
        }
        //
        webServiceResponse.setErrorCode("0");
        webServiceResponse.setErrorMsg("OK");
        webServiceResponse.setResultData(null);
        return webServiceResponse;
    }

    public WebServiceResponse CallAgv(CallAgv mod) {
        WebServiceResponse webServiceResponse = new WebServiceResponse();
        String timestampNow = LocalDateTime.now().format(dfISO);
        //
        BerthState now = berthStateMapper.GetMode(mod.getMacCode(), mod.getBerthCode());
        if (now == null) {
            now = new BerthState();
            now.setMacCode(mod.getMacCode());
            now.setBerthCode(mod.getBerthCode());
        }
        now.setLastTime(timestampNow);
        //
        if (mod.getWipEntity() == null) {
            //不传工单号为操作进料区
            now.setArea("IN");
            //传进料位，不传工单号时，为叫料到进料位
            //再次传进料位，不传工单号时，为移走进料位空车
            if (StringUtils.isEmpty(now.getWipEntity())) {
                //当前料位无工单时，此动作为叫料到进料区
                String lotNum = GenerateWipEntity(1, mod.getMacCode());
                now.setWipEntity(lotNum);
                now.setOpName("进料区叫料");
            } else {
                //当前料位有工单时，此动作出空车
                now.setWipEntity("");
                now.setOpName("进料区出空车");
            }
        } else {
            //有传工单时为操作出料区
            now.setArea("OUT");
            now.setWipEntity(mod.getWipEntity());
            //
            if (!StringUtils.isEmpty(mod.getWipEntity())) {
                //工单号不空时为移走出料区满货架
                now.setOpName("出料区出满车");
            } else {
                //工单号为空时为呼叫空货架到出料区
                now.setOpName("出料区叫空车");
            }
        }
        berthStateMapper.SetData(now);
        berthStateHisMapper.insertMod2(now);
        //
        webServiceResponse.setErrorCode("0");
        webServiceResponse.setErrorMsg("OK");
        webServiceResponse.setResultData(com.alibaba.fastjson.JSON.toJSONString(now, true));
        return webServiceResponse;
    }

    DateTimeFormatter dfWIP = DateTimeFormatter.ofPattern("yyMMddHHmmssSSS");

    public String GenerateWipEntity(int groupId, String macCode) {
        String lotnum = "Lot" + LocalDateTime.now().format(dfWIP);
        //
        String d = lotnum.substring(lotnum.length() - 1);
        int i = Integer.parseInt(d);
        //int groupId = (i % 3) + 1;
        List<TagBasic> list = wipEntityTagMapper.selectTagsByGroupId(groupId);
        //
        for (TagBasic tag : list) {
            WipEntityTag wip = new WipEntityTag();
            wip.setWipEntity(lotnum);
            wip.setTagCode(macCode + "_" + tag.getTagCode());
            wip.setTagDesc(tag.getTagDesc());
            wip.setTagValue(tag.getDefValue());
            wipEntityTagMapper.insertWipItem(wip);
        }
        //
        return lotnum;
    }
}